package cn.anecansaitin.whimsy.common.effect.ow.doomfist;

import cn.anecansaitin.whimsy.client.registers.ModSounds;
import cn.anecansaitin.whimsy.common.attachment.ImpactData;
import cn.anecansaitin.whimsy.common.attachment.SpeedData;
import cn.anecansaitin.whimsy.common.effect.IEffectFinishes;
import cn.anecansaitin.whimsy.common.register.ModDamageSources;
import cn.anecansaitin.whimsy.common.register.ModDataAttachments;
import cn.anecansaitin.whimsy.common.register.ModEffects;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.sounds.SoundSource;
import net.minecraft.util.Mth;
import net.minecraft.world.effect.MobEffect;
import net.minecraft.world.effect.MobEffectCategory;
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.phys.Vec3;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.neoforge.event.entity.living.MobEffectEvent;

public class Impact extends MobEffect implements IEffectFinishes {
    public Impact() {
        super(MobEffectCategory.HARMFUL, 0);
    }

    @Override
    public void applyEffectTick(LivingEntity livingEntity, int amplifier) {
        Vec3 velocity = livingEntity.getDeltaMovement();
        CompoundTag tag = livingEntity.getPersistentData();

        if (!tag.contains("rock_punch", CompoundTag.TAG_COMPOUND)) {
            return;
        }

        CompoundTag rockData = tag.getCompound("rock_punch");

        //如果速度小于一定值就停止技能，相当于撞墙了
        if (Mth.sqrt((float) ((velocity.x * velocity.x) + (velocity.z * velocity.z))) < 0.5f) {
            livingEntity.removeEffect(ModEffects.IMPACT.get());
            livingEntity.hurt(ModDamageSources.owNoKnockback(livingEntity.level(), livingEntity, null, livingEntity.position()), rockData.getFloat("d"));
            livingEntity.level().playSound(null, livingEntity.getX(), livingEntity.getY(), livingEntity.getZ(), ModSounds.ROCKET_PUNCH_ENEMY_CRASH_WALL.get(), livingEntity.getSoundSource());
        } else {
            livingEntity.setDeltaMovement(rockData.getFloat("x"), 0, rockData.getFloat("z"));
        }
    }

    @Override
    public boolean shouldApplyEffectTickThisTick(int duration, int amplifier) {
        return true;
    }

    public void onEffectFinished(LivingEntity entity) {
        entity.setDeltaMovement(0, 0, 0);
    }

    @Override
    public boolean isPlayerClientEffect() {
        return true;
    }
}